//
//  InitiatePollingViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/2/27.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "InitiatePollingViewController.h"
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import "ScreentoneViewController.h"
#import "PollingDetailViewController.h"
@interface InitiatePollingViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
{
    BOOL isGeoSearch;
    double longitude;
    double latitude;
    //机构
    NSString *_uuid;
}
@property(nonatomic,strong) UILabel *selectLab;
@property(nonatomic,strong)BMKLocationService* locService;
@property(nonatomic,strong)BMKGeoCodeSearch *geocodesearch;
@property(nonatomic,strong)UILabel *describeLab;
@property(nonatomic,strong)UIView *centerView;
@property(nonatomic,strong)UIScrollView*scrollView;

@end

@implementation InitiatePollingViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];


}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"发起巡检"];
//    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
    _geocodesearch = [[BMKGeoCodeSearch alloc] init];
    //编码服务的初始化(就是获取经纬度,或者获取地理位置服务)
    _geocodesearch.delegate = self;//设置代理为self
     _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 74, WindowWidth, WindowHeight-74)];
    _scrollView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_scrollView];
    UILabel *line1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 0.5)];
    line1.backgroundColor=LineColor ;
    [_scrollView addSubview:line1];
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(15, 0.5, WindowWidth-30, 45)];
//    headView.backgroundColor=[UIColor redColor];
    [_scrollView addSubview:headView];
    UILabel *nameLab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 45)];
    nameLab.text=@"巡检机构";
    nameLab.textColor=FontColor;
    nameLab.font=F(15);
    [headView addSubview:nameLab];
    _selectLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(nameLab.frame)+10, 0, headView.bounds.size.width-CGRectGetMaxX(nameLab.frame)-10, 45)];
    _selectLab.text=@"请选择";
    _selectLab.textColor=MainColor;
    _selectLab.font=F(15);
    [headView addSubview:_selectLab];
    _selectLab.userInteractionEnabled=YES;
    [_selectLab bk_whenTapped:^{
        ScreentoneViewController *screenVC=[[ScreentoneViewController alloc]init];
        screenVC.backuiud=^(NSMutableDictionary*dic){
            _selectLab.text =[dic objectForKey:@"name"];
            _uuid =[dic objectForKey:@"uuid"];
            longitude=[[dic objectForKey:@"longitude"]doubleValue];
            latitude=[[dic objectForKey:@"latitude"] doubleValue];

//            [self showData];
        };
        [self pushController:screenVC];
    }];
    UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(headView.frame), WindowWidth, 0.5)];
    line.backgroundColor=LineColor ;
    [_scrollView addSubview:line];
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    [_scrollView addSubview:button];
    button.frame=CGRectMake(29,CGRectGetMaxY(line.frame)+100, WindowWidth-58, 40);
    [button setBackgroundColor:MainColor];
    button.layer.masksToBounds=YES;
    button.layer.cornerRadius=8;
    button.userInteractionEnabled=YES;

    [button setTitle:@"开始巡检" forState:UIControlStateNormal];
    [button bk_addEventHandler:^(id sender) {
        if (_uuid==nil||[_uuid isEqualToString:@""]) {
            //没有机构
            [CClass showMessage:@"请选择巡检机构"];
            return ;
        }

        [CClass HttpRequestTask:YES withUrl:@"inspection_createAndStart" withData:@{@"inst_uuid":_uuid} notifyError:YES onSuccess:^(BaseModel *model) {
            button.userInteractionEnabled=NO;

            if([F_NSpace([model.B_data objectForKey:@"uuid"]) isEqualToString:@""]&&[F_NSpace([model.B_data objectForKey:@"uuid"]) isEqualToString:@""]){
            }else
            {
                PollingDetailViewController *detailVc =[[PollingDetailViewController alloc]init];
                detailVc.warningUuid=[model.B_data objectForKey:@"uuid"];
                detailVc.type=2;
                detailVc.state=@"10";//model.H_state;
                [self pushController:detailVc Lever:1 Animated:YES];
            }
           
        } onError:^(BaseModel *model) {
            button.userInteractionEnabled=YES;
        }];
      
    } forControlEvents:UIControlEventTouchUpInside];
//    [self showData];
}
- (void)showData
{
    [_centerView removeFromSuperview];
    _centerView=[[UIView alloc]initWithFrame:CGRectMake(0,  46, WindowWidth, _scrollView.bounds.size.height- 70.5)];
    _centerView.backgroundColor=[UIColor whiteColor];
    [_scrollView addSubview:_centerView];

    _describeLab=[[UILabel alloc]initWithFrame:CGRectMake(15, 15, WindowWidth-30, 30)];
    _describeLab.textColor=FontColor;
    _describeLab.font=F(12);
    [_centerView addSubview:_describeLab];
    CGFloat height;
    __block typeof (height)blockSelf=height;
    blockSelf=CGRectGetMaxY(_describeLab.frame);

    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    [_centerView addSubview:button];
    [button setBackgroundColor:MainColor];
    button.layer.masksToBounds=YES;
    button.layer.cornerRadius=8;
    button.userInteractionEnabled=YES;
    [button setTitle:@"开始巡检" forState:UIControlStateNormal];
    [button bk_addEventHandler:^(id sender) {
        if (_uuid==nil||[_uuid isEqualToString:@""]) {
            //没有机构
            [CClass showMessage:@"请选择巡检机构"];
            return ;
        }

        [CClass HttpRequestTask:YES withUrl:@"inspection_createAndStart" withData:@{@"inst_uuid":_uuid} notifyError:YES onSuccess:^(BaseModel *model) {
            button.userInteractionEnabled=NO;

        } onError:^(BaseModel *model) {
            button.userInteractionEnabled=YES;

        }];
//        PollingDetailViewController *detailVc =[[PollingDetailViewController alloc]init];
//        detailVc.warningUuid=@"cce321de29524d859cf8ebde70af9e1a";//model.H_uuid;
//        detailVc.type=2;
//        detailVc.state=@"02";//model.H_state;
//        [self pushController:detailVc];
    } forControlEvents:UIControlEventTouchUpInside];
    if ( [CClass isLocationServiceOpen]==YES) {
    
        [CClass HttpRequestTask:YES withUrl:@"inspection_pagedOperationst" withData:@{@"uuid":@"262a9301c34b451db946a771a2ac964f"} notifyError:YES onSuccess:^(BaseModel *model) {
            NSMutableArray *dataAr=[[NSMutableArray alloc]init];
            dataAr=(NSMutableArray*)[model.B_data objectForKey:@"list"];
           
            if (dataAr.count==0) {
                _describeLab.text=@"没有巡检记录。";
                button.frame=CGRectMake(29,200, WindowWidth-58, 40);

                
            }else
            {
                _describeLab.text=@"本季度已巡检。";

                //名字
                UILabel*agentLab= [[UILabel alloc ]initWithFrame:CGRectMake(15, blockSelf, WindowWidth-30-75-15, 40)];
                agentLab.userInteractionEnabled=YES;
                agentLab.font=F(12);
                agentLab.textColor=FontColor;
                [_centerView addSubview:agentLab];
                agentLab.text=@"未开始的巡检 安排给：吴家颖";
                UILabel*gotoLab= [[UILabel alloc ]initWithFrame:CGRectMake(CGRectGetMaxX(agentLab.frame)+10, blockSelf, 75, 30)];
                gotoLab.userInteractionEnabled=YES;
                gotoLab.font=F(14);
                gotoLab.textAlignment=NSTextAlignmentCenter;
                gotoLab.backgroundColor=MainColor;
                gotoLab.text=@"代为处理";
                gotoLab.layer.cornerRadius=4;
                gotoLab.layer.masksToBounds=YES;
                gotoLab.textColor=[UIColor whiteColor];
                [_centerView addSubview:gotoLab];
//                button.hidden=YES;

                [gotoLab bk_whenTapped:^{
                    PollingDetailViewController *detailVc =[[PollingDetailViewController alloc]init];
                    detailVc.warningUuid=@"cce321de29524d859cf8ebde70af9e1a";//model.H_uuid;
                    detailVc.type=2;
                    detailVc.state=@"02";//model.H_state;
                    [self pushController:detailVc];
                }];
                blockSelf=blockSelf+40;
                button.frame=CGRectMake(29,dataAr.count*30+blockSelf+20, WindowWidth-58, 40);

            }

            for (int i=0; i<dataAr.count; i++) {
                
                UILabel*timeLab=[[UILabel alloc]initWithFrame:CGRectMake(15,blockSelf+30*i, 100, 30)];
                [_centerView addSubview:timeLab];
                
                timeLab.font=F(12);
                timeLab.textColor=FontColor;
                timeLab.text=@"2017-02-09";
                
                //名字
                UILabel*nameLab= [[UILabel alloc ]initWithFrame:CGRectMake(CGRectGetMaxX(timeLab.frame), blockSelf+30*i, WindowWidth-CGRectGetMaxX(timeLab.frame)-15, 30)];
                nameLab.userInteractionEnabled=YES;
                [_centerView addSubview:nameLab];
                nameLab.font=F(12);
                nameLab.userInteractionEnabled =YES;
                nameLab.textColor=FontColor;
                nameLab.text =@"哈哈";
                [_centerView addSubview:nameLab];

            }
            
            } onError:^(BaseModel *model) {

            }];
  
    }else
    {
        
        if (_uuid==nil||[_uuid isEqualToString:@""]) {
            //没有机构
            _describeLab.text=@"请选择一家机构，查看该机构的巡检记录。";
            button.hidden=YES;
            
        }else
        {
            //有机构
            button.hidden=NO;
            _describeLab.text=@"没有巡检记录。";
          

            [CClass HttpRequestTask:YES withUrl:@"inspection_pagedOperationst" withData:@{@"uuid":@"262a9301c34b451db946a771a2ac964f"} notifyError:YES onSuccess:^(BaseModel *model) {
                NSMutableArray *dataAr=[[NSMutableArray alloc]init];
                dataAr=(NSMutableArray*)[model.B_data objectForKey:@"list"];
                if (dataAr.count==0) {
                    _describeLab.text=@"没有巡检记录。";
                    button.frame=CGRectMake(29,200, WindowWidth-58, 40);

                }else
                {
                    
                    _describeLab.text=@"本季度已巡检。";
                    //名字
                    UILabel*agentLab= [[UILabel alloc ]initWithFrame:CGRectMake(15, blockSelf, WindowWidth-30-75-15, 40)];
                    agentLab.userInteractionEnabled=YES;
                    agentLab.font=F(12);
                    agentLab.textColor=FontColor;
                    [_centerView addSubview:agentLab];
                    agentLab.text=@"未开始的巡检 安排给：吴家颖";
                    UILabel*gotoLab= [[UILabel alloc ]initWithFrame:CGRectMake(CGRectGetMaxX(agentLab.frame)+10, blockSelf, 75, 30)];
                    gotoLab.userInteractionEnabled=YES;
                    gotoLab.font=F(14);
                    gotoLab.textAlignment=NSTextAlignmentCenter;
                    gotoLab.backgroundColor=MainColor;
                    gotoLab.text=@"代为处理";
                    gotoLab.layer.cornerRadius=4;
                    gotoLab.layer.masksToBounds=YES;
                    gotoLab.textColor=[UIColor whiteColor];
                    [_centerView addSubview:gotoLab];
//                    button.hidden=YES;
                    
                    [gotoLab bk_whenTapped:^{
                   PollingDetailViewController *detailVc =[[PollingDetailViewController alloc]init];
                        detailVc.warningUuid=@"cce321de29524d859cf8ebde70af9e1a";//model.H_uuid;
                        detailVc.type=2;
                        detailVc.state=@"02";//model.H_state;
                        [self pushController:detailVc];

                    }];
                    blockSelf=blockSelf+40;
//                    button.hidden=YES;
                    button.frame=CGRectMake(29,dataAr.count*30+blockSelf+20, WindowWidth-58, 40);

                }
                for (int i=0; i<dataAr.count; i++) {
                    
                    UILabel*timeLab=[[UILabel alloc]initWithFrame:CGRectMake(15,blockSelf+30*i, 100, 30)];
                    [_centerView addSubview:timeLab];
                    
                    timeLab.font=F(14);
                    timeLab.textColor=FontColor;
                    timeLab.text=@"2017-02-09";
                    
                    //名字
                    UILabel*nameLab= [[UILabel alloc ]initWithFrame:CGRectMake(CGRectGetMaxX(timeLab.frame), blockSelf+30*i, WindowWidth-CGRectGetMaxX(timeLab.frame)-15, 30)];
                    nameLab.userInteractionEnabled=YES;
                    [_centerView addSubview:nameLab];
                    nameLab.font=F(14);
                    nameLab.userInteractionEnabled =YES;
                    nameLab.textColor=FontColor;
                    nameLab.text =@"哈哈";
                    [_centerView addSubview:nameLab];
                    
                }
            } onError:^(BaseModel *model) {
                
            }];

            
            
            
        }
        
        
    }
}
#pragma mark 定位

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //关闭地位
    
    
    [self onClickReverseGeocode];
    [_locService stopUserLocationService];
}
-(void)onClickReverseGeocode  //发送反编码请求的.
{
    isGeoSearch = false;
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};//初始化
    if (_locService.userLocation.location.coordinate.longitude!= 0
        && _locService.userLocation.location.coordinate.latitude!= 0) {
        //如果还没有给pt赋值,那就将当前的经纬度赋值给pt
        pt = (CLLocationCoordinate2D){_locService.userLocation.location.coordinate.latitude,
            _locService.userLocation.location.coordinate.longitude
        };
    }
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];//初始化反编码请求
    reverseGeocodeSearchOption.reverseGeoPoint = pt;//设置反编码的店为pt
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];//发送反编码请求.并返回是否成功
    if(flag)
    {
        echo(@"反geo检索发送成功");
    }
    else
    {
        echo(@"反geo检索发送失败");
    }
}
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    
    //    _cityName = result.addressDetail.city;
    //    _province = result.addressDetail.province;
    for (int i = 0; i < result.poiList.count; i++) {
        
        BMKPoiInfo* poi = [result.poiList objectAtIndex:i];
        if (i==0) {
            //            _locationLab.text = poi.name;
            latitude= poi.pt.latitude;
            longitude = poi.pt.longitude;
#pragma mark 机构请求
            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
            [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
            [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
            [CClass HttpRequestTask:YES withUrl:@"select_showInstSelect" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                NSMutableArray *ar=[model.B_data objectForKey:@"list"];
                NSDictionary *dic =[ar objectAtIndex:0];
                _uuid=[dic objectForKey:@"uuid"];
                _selectLab.text=[dic objectForKey:@"name"];
                
                
            } onError:^(BaseModel *model) {
                
            }];
            
        }
    }
}



@end
